Page({
  data: {
    newsList: [
      {
        id: 1,
        tag: '新品',
        title: '玉米秸秆饲料',
        description: '玉米秸秆饲料。',
        imageUrl:   '/assets/images/1.jpg',
        time: '2023-12-20'
      },
      {
        id: 2,
        tag: '活动',
        title: '玉米秸秆饲料',
        description: '玉米秸秆饲料',
        imageUrl:   '/assets/images/2.jpg',
        time: '2023-12-19'
      },
      {
        id: 3,
        tag: '促销',
        title: '玉米秸秆饲料',
        description: '玉米秸秆饲料',
        imageUrl:   '/assets/images/3.jpg',
        time: '2023-12-18'
      }
    ],
    loading: false,
    noMore: false,
    page: 1
  },

  onLoad() {
    this.loadNews()
  },

  // 加载新闻数据
  loadNews() {
    if (this.data.loading || this.data.noMore) return
    
    this.setData({ loading: true })
    // TODO: 从服务器加载新闻数据
    setTimeout(() => {
      this.setData({ 
        loading: false,
        noMore: true
      })
    }, 1000)
  },

  // 查看新闻详情
  onNewsDetail(e) {
    const newsId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/news/detail?id=${newsId}`
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      newsList: [],
      page: 1,
      noMore: false
    }, () => {
      this.loadNews()
      wx.stopPullDownRefresh()
    })
  },

  // 上拉加载更多
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    }, () => {
      this.loadNews()
    })
  }
}) 